package com.jinbei.restController;

import com.github.pagehelper.PageInfo;
import com.jinbei.common.Enum.ConstantsEnum;
import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.common.base.ResponseBase;
import com.jinbei.common.base.ResponseBaseEntity;
import com.jinbei.common.contants.Contants;
import com.jinbei.domain.Role;
import com.jinbei.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zflu
 * 角色 restful 接口
 * Created by lucky on 2018/7/9.
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    private final Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    /**
     * @author zflu
     * @Description  根据角色id查询角色
     * @date 2018/7/9 15:41
     * @param roleId
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/role/queryRole" ,logs = "角色查询")
    @GetMapping("/queryRole")
    public ResponseBase queryRole(@RequestParam("roleId") int roleId){
        try {
            Role role = roleService.queryRoleByRoleId(roleId);
            return ResponseBaseEntity.setResultSuccess(role);
        }catch (Exception e){
            logger.error("查询角色详情失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   角色新增
     * @date 2018/7/9 15:40
     * @param role
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/role/insertRole" ,logs = "角色新增")
    @PostMapping("/insertRole")
    public ResponseBase insertRole(@RequestBody Role role){
        try {
            int result = roleService.insertRole(role);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("角色新增失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   根据角色id删除角色
     * @date 2018/7/9 15:40
     * @param roleId
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/role/deleteRole" ,logs = "角色删除")
    @DeleteMapping("/deleteRole")
    public ResponseBase insertRole(@RequestParam("roleId") int roleId){
        try {
            int result = roleService.deleteRoleByRoleId(roleId);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("删除角色失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description 角色修改
     * @date 2018/7/9 15:40
     * @param role
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/role/updateRole" ,logs = "角色修改")
    @PutMapping("/updateRole")
    public ResponseBase updateRole(@RequestBody Role role){
        try {
            int result = roleService.updateRole(role);
            if(result > 0){
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_TRUE);
            }else{
                return ResponseBaseEntity.setResultSuccess(Contants.BOOLEAN_FALSE);
            }
        }catch (Exception e){
            logger.error("角色修改失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

    /**
     * @author zflu
     * @Description   查询角色分页列表
     * @date 2018/7/12 9:25
     * @param pageNum, pageSize
     * @return com.jinbei.common.base.ResponseBase
     */
    @AuthorAnnotation(name = "/role/queryRolePage" ,logs = "角色查询分页")
    @GetMapping("/queryRolePage")
    public ResponseBase queryRolePage(@RequestParam("pageNum") int pageNum,
                                      @RequestParam("pageSize") int pageSize ){
        try {
            PageInfo pageInfo = roleService.queryRolePage(pageNum, pageSize);
            return ResponseBaseEntity.setResultSuccess(pageInfo);
        }catch (Exception e){
            logger.error("查询角色分页列表失败!  {}",e);
            return ResponseBaseEntity.setResultError(null);
        }
    }

}
